package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.PageResult;
import com.pinyougou.pojo.Result;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@RestController
@RequestMapping("/brand")
public class BrandController {
    @Reference
    private BrandService brandService;
    //1.查询所有品牌
    @RequestMapping("/list")
    public List<TbBrand> findAll(){
        return brandService.findAll();
    }

    //2.分页查询所有品牌
    @RequestMapping("/findByPage")
    public PageResult findByPage(int page, int pagesize){
        return brandService.findByPage(page,pagesize);
    }

    //3.分页待条件查询品牌列表
    @RequestMapping("/search")
    public PageResult search(int page, int pagesize, @RequestBody(required = false) TbBrand brand){
        return brandService.search(page,pagesize,brand);
    }
    //4.添加品牌
    @RequestMapping("/add")
    public Result add(@RequestBody TbBrand brand){
        System.out.println("aaa"+brand.getName());
        try{
            brandService.add(brand);
            return new Result(true,"添加品牌成功！");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"添加品牌失败！");
        }
    }

    //5.修改品牌
    @RequestMapping("/update")
    public Result update(@RequestBody TbBrand brand){
        try{
            brandService.update(brand);
            return new Result(true,"修改品牌成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"修改品牌失败");
        }
    }

    //6.根据用户选择的品牌id列表删除多个品牌
    @RequestMapping("/delete")
    public Result delete(String[] ids){
        try {
            brandService.delete(ids);
            return new Result(true,"删除品牌成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"删除品牌失败");
        }
    }
}
